package com.twu.biblioteca.menu;

import com.twu.biblioteca.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MemberShip implements MenuOption {
    private MemberShip memberShipInstance;


    public final MemberShip getMemberShipInstance() {
        if(memberShipInstance == null){
            memberShipInstance = new MemberShip();
        }
        return memberShipInstance;
    }

    @Override
    public void execute() {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        LibraryManager libraryManager = LibraryManager.getInstance();
        if (libraryManager.isLoggedIn()) {
            libraryManager.printToScreen(libraryManager.loggedInUser.toString());
        } else {
            libraryManager.printToScreen(Messages.NO_USERS_FOUND);
            libraryManager.printToScreen(Messages.REQUEST_USER_NAME + " and " + Messages.REQUEST_USER_PASSWORD);
            try {
                libraryManager.authenticateUser(scanner.readLine(), scanner.readLine());
                libraryManager.printToScreen(libraryManager.getOutput());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        MemberShip that = (MemberShip) o;

        if (memberShipInstance != null ? !memberShipInstance.equals(that.memberShipInstance) : that.memberShipInstance != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return memberShipInstance != null ? memberShipInstance.hashCode() : 0;
    }
}
